// TabControl.cpp: implementation of the TabControl class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TabControl.h"
#include "TabControlPeer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TabControl::TabControl()
{
	m_cmpActive = NULL;
}

TabControl::~TabControl()
{

}

CWnd* TabControl::createComponent()
{
	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	TabControlPeer* pwnd = new TabControlPeer(this);
	CWnd* wndParent = getParent()->getPeer();
	if (wndParent == NULL)
		return NULL;
	pwnd->Create(WS_CHILD | WS_VISIBLE, getBounds(), wParent, 100);
	pwnd->SetFont(CFont::FromHandle(m_font));

	for (int n = 0; n < getComponentCount(); n++)
	{
		AWTComponent* cmp = getComponent(n);
		pwnd->InsertItem(n, cmp->getName());
	}

	return pwnd;
}

void TabControl::setActivePanel(int index)
{
	if (index > getComponentCount())
		return;

	TabControlPeer* peer = (TabControlPeer*)getPeer();
	
	CRect rc;
	peer->GetClientRect(&rc);
	peer->AdjustRect(FALSE, &rc);

	if (m_cmpActive != NULL)
	{
		m_cmpActive->setVisible(FALSE);
	}

	if ((m_cmpActive = getComponent(index)) != NULL)
	{
		m_cmpActive->setVisible(TRUE);
		m_cmpActive->setBounds(rc);
		m_cmpActive->validate();
	}
}

void TabControl::peerResized()
{
	if (m_cmpActive == NULL)
		return;
	TabControlPeer* peer = (TabControlPeer*)getPeer();
	if (peer == NULL)
		return;
//	if (!IsWindow(peer->m_hWnd))
//		return;
	CRect rc;
	peer->GetClientRect(&rc);
	peer->AdjustRect(FALSE, &rc);
	m_cmpActive->setBounds(rc);
	m_cmpActive->validate();
}

void TabControl::validate() 
{
	if (getComponentCount() == 0)
		return;

	if (m_cmpActive == NULL)
	{
		setActivePanel(0);
	}
}
